package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 逸尘风清
 * @date 2021/4/6
 * @time 15:57
 * 描述：
 */
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    // 根据dictCode获取下级节点
    @ApiOperation("根据dictCode获取下级节点")
    @GetMapping("findChildByDictCode/{dictCode}")
    public Result findChildByDictCode(@PathVariable String dictCode){
        List<Dict> dictList = dictService.findChildByDictCode(dictCode);
        return Result.ok(dictList);
    }

    // 根据dictCode和value 获取名称
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode, @PathVariable String value){
        String dictName = dictService.getName(dictCode, value);
        return dictName;
    }

    // 根据value 获取名称
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value){
        String dictName = dictService.getName("", value);
        return dictName;
    }

    //导入数据字典
    @PostMapping("importData")
    public Result importDict(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }


    @ApiOperation(value = "Excel导出", notes = "export", produces = "application/octet-stream")  // produces请求头格式，防止导出的文件无法打开
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportDictData(response);
    }

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChlidData(id);
        return Result.ok(list);
    }
}
